package com.tian.service.impl.management;


import com.tian.common.enums.ResultCode;
import com.tian.common.util.Result;
import com.tian.dto.management.req.RoleMenuReqDto;
import com.tian.entity.RoleMenu;
import com.tian.mapper.RoleMenuMapper;
import com.tian.service.management.RoleMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * {@code @description:} 角色权限服务实现类
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-09-10 8:32
 * {@code @version:} 1.0
 */
@Slf4j
@Service
public class RoleMenuServiceImpl implements RoleMenuService {
    @Resource
    private RolePermissionService rolePermissionService;
    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<Boolean> add(RoleMenuReqDto roleMenuReqDto) {
        //先删除原有关系
        roleMenuMapper.deleteByRoleId(roleMenuReqDto.getRoleId());
        //再插入新关系
        String menuIds = roleMenuReqDto.getMenuIds();
        //[[1],[2],[2,4],[2],[2,4]]
        HashSet<Integer> menuIdSet = new HashSet<>();
        if (!StringUtils.hasText(menuIds)||"null".equals(menuIds)) {
            return Result.success(Boolean.TRUE);
        }
        try {
            String splitStr = ",";
            String[] split = menuIds.split(splitStr);
            for (String menuId : split) {
                if (menuId.contains("[[") || menuId.contains("]]")) {
                    menuId = menuId.replace("[[", "[").replace("]]", "]");
                }
                String[] split1 = menuId.split(splitStr);
                for (String menuId1 : split1) {
                    if (!menuId1.contains(",")) {
                        menuId1 = menuId1.replace("[", "").replace("]", "");
                        menuIdSet.add(Integer.parseInt(menuId1));
                    } else {
                        String[] split2 = menuId1.split(splitStr);
                        for (String menuId2 : split2) {
                            menuId2 = menuId2.replace("[", "").replace("]", "");
                            menuIdSet.add(Integer.parseInt(menuId2));
                        }
                    }
                }

            }
        }catch (NumberFormatException e){
            log.error("参数有误",e);
            return Result.fail(ResultCode.MENU_NOT_NULL);
        }

        log.info("menuIdSet:{}", menuIdSet);
        List<RoleMenu> roleMenuList = new ArrayList<>();
        for (Integer menuId : menuIdSet) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleMenuReqDto.getRoleId());
            roleMenu.setMenuId(menuId);
            roleMenuList.add(roleMenu);
        }
        //批量插入
        roleMenuMapper.saveBatch(roleMenuList);
        rolePermissionService.initCache();
        return Result.success(Boolean.TRUE);
    }
}
